package com.swsc.solarverse.common.util;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * @Author anonymous
 * @Date 2021/4/29 21:50
 * @Version: 1.0
 * @Description
 */
public class CalculateUtils {
	private final static BigDecimal BIG_DECIMAL_HUNDRED = new BigDecimal("100");

	public static Float multiplyNullZero(Float f1, Float f2){
		if(null == f1 || null == f2 || f1 == 0 || f2 == 0){
			return 0f;
		}
		return f1 * f2;
	}
	public static Float multiplyFloat(Float f1, Float f2){
		if(null == f1 || null == f2){
			return null;
		}
		return f1 * f2;
	}
	public static Float multiplyFloat(Float f1, Float f2, int scale){
		if(null == f1 || null == f2){
			return null;
		}
		Float f = f1 * f2;
		return new BigDecimal(f).setScale(scale, RoundingMode.HALF_UP).floatValue();
	}

	public static Double multiplyDouble(Double d1, Double d2){
		if(null == d1 || null == d2){
			return null;
		}
		return d1 * d2;
	}

	public static Double multiplyDouble(Double d1, Double d2, int scale){
		if(null == d1 || null == d2){
			return null;
		}
		Double res =  d1 * d2;
		if(scale < 0){
			return res;
		}
		return new BigDecimal(res).setScale(scale, RoundingMode.HALF_UP).doubleValue();
	}

	public static Double setPowerUnit(Double power, Double unit, int scale){
		if(null == power){
			 return null;
		}
		Double res = power;
		if(null != unit){
			res = power * unit;
		}
		if(scale < 0){
			return res;
		}
		return new BigDecimal(res).setScale(scale, RoundingMode.HALF_UP).doubleValue();
	}

	public static Float multiplyFloatInt(Float f1, Integer i1){
		if(null == f1 || null == i1){
			return null;
		}
		return f1 * i1;
	}
	public static Float div(Float f1, Float f2, int scale){
		if(null != f1 && null != f2 && f2 != 0){
			String s1 = Float.toString(f1);
			String s2 = Float.toString(f2);
			return new BigDecimal(s1).divide(new BigDecimal(s2), scale, BigDecimal.ROUND_HALF_UP).floatValue();
		}
		return 0f;
	}
	public static Float div(Float f1, Float f2){
		if(null != f1 && null != f2 && f2 != 0){
			return f1 / f2;
		}
		return 0f;
	}
	public static Float div(Integer n1, Integer n2){
		if(null != n1 && null != n2 && n2 != 0){
			return Float.valueOf(n1) / Float.valueOf(n2);
		}
		return 0f;
	}

	public static Double divNullZero(Double d1, Double d2){
		if(null != d1 && null != d2 && d2 != 0){
			return d1 / d2;
		}
		return 0D;
	}
	public static Integer divNull(Integer i1, Integer i2){
		if(null == i1 || null == i2){
			return null;
		}
		return i1 / i2;
	}
	public static Double subNullZero(Double d1, Double d2){
		if(null != d1 && null != d2){
			return d1 - d2;
		}
		return 0D;
	}

	public static Float calRate(Integer n1, Integer n2, int scale){
		if(null == n1 || n1 == 0){
			return 0f;
		}
		if(null == n2 || n2 == 0){
			return 100f;
		}
		return calFloatRate(n1.toString(), n2.toString(), scale);
	}

	public static Float calRate(Float f1, Float f2, int scale){
		if(null == f1 || f1 == 0){
			return 0f;
		}
		if(null == f2 || f2 == 0){
			return 100f;
		}
		return calFloatRate(Float.toString(f1), Float.toString(f2), scale);
	}

	public static Float calFloatRate(String s1, String s2, int scale){
		if(null == s1 || "".equals(s1.trim())){
			return 0f;
		}
		if(null == s2 || "".equals(s2.trim())){
			return 100f;
		}
		return new BigDecimal(s1).divide(new BigDecimal(s2), 10, BigDecimal.ROUND_HALF_UP).multiply(BIG_DECIMAL_HUNDRED, new MathContext(scale, RoundingMode.HALF_UP)).floatValue();
	}

	public static Double calDoubleRate(Double d1, Double d2, int scale){
		if(null == d1){
			return 0D;
		}
		if(null == d2 || d2 == 0D){
			return 100D;
		}
		BigDecimal b1 = new BigDecimal(Double.toString(d1));
		BigDecimal b2 = new BigDecimal(Double.toString(d2));
		double resultValue=b1.multiply(BIG_DECIMAL_HUNDRED).divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
		return  resultValue;
		//防止出现科学计数法
//		BigDecimal bigDecimal1 = new BigDecimal(resultValue);
//		String finishResult= bigDecimal1.setScale(5, BigDecimal.ROUND_HALF_UP).toString();
//		return finishResult;

	}

	public static Double multiply(String s1, String s2, int scale) {
		BigDecimal b1 = new BigDecimal(s1);
		BigDecimal b2 = new BigDecimal(s2);
		BigDecimal res = b1.multiply(b2);
		res = res.setScale(scale, RoundingMode.HALF_UP);
		return res.doubleValue();
//        return new BigDecimal(s1).multiply(new BigDecimal(s2), new MathContext(scale, RoundingMode.HALF_UP)).doubleValue();
	}

	public static Double multiply(Double d1, Double d2, int scale) {
		BigDecimal b1 = BigDecimal.valueOf(d1);
		BigDecimal b2 = BigDecimal.valueOf(d2);
		BigDecimal res = b1.multiply(b2);
		res = res.setScale(scale, RoundingMode.HALF_UP);
//        return multiply(Double.toString(d1), Double.toString(d2), scale);
		return res.doubleValue();
	}
	public static Integer multiplyNull(Integer i1, Integer i2){
		if(null == i1 || null == i2){
			return null;
		}
		return i1 * i2;
	}

	public static Double calElec(Double power) {
		if(null == power){
			return null;
		}
		return power / 3600000D;
//        return div(Double.toString(power), "3600000", 4);
	}

	public static Double div(String s1, String s2, int scale){
		return new BigDecimal(s1).divide(new BigDecimal(s2), new MathContext(scale, RoundingMode.HALF_UP)).doubleValue();
	}

	public static Float plus(Float f1, Float f2){
		if(null == f1 && null == f2){
			return null;
		}
		if(null == f1){
			return f2;
		}
		if(null == f2){
			return f1;
		}
		return f1 + f2;
	}

	public static Double plus(Double d1, Double d2){
		if(null == d1 && null == d2){
			return null;
		}
		if(null == d1){
			return d2;
		}
		if(null == d2){
			return d1;
		}
		return d1 + d2;
	}

	public static Double calTheoryPower(Double capacity, Double irradiation){
		// 理论发电量 = 装机容量(kW) * 辐射量(kW·h/m2) * 系统综合效率(约等于0.8)
		return calTheoryPower(capacity, irradiation, 1D);
	}

	// 装机容量*标准日照时长*系统效率
	// (辐射量/3.6) * 1000 * 装机容量
	public static Double calTheoryPower(Double capacity, Double irradiation, Double unit){
		// 理论发电量 = 装机容量(kW) * 辐射量(kW·h/m2) * 系统综合效率(约等于0.8)
		Double power = capacity * irradiation * 0.8D;
		return setPowerUnit(power, unit, 4);
	}

	/**
	 * 计算节约标煤
	 * @param power
	 * @return
	 */
	public static Double calReduceCoal(Double power){
		if(null == power){
			return null;
		}
		return setPowerUnit(power, 0.36D, 2);
	}

	/**
	 * 二氧化碳减排
	 * @param power
	 * @return
	 */
	public static Double calReduceCO2(Double power){
		if(null == power){
			return null;
		}
		return setPowerUnit(power, 0.997D, 2);
	}

	public static Integer plus(Integer i1, Integer i2){
		if(null == i1 && null == i2){
			return null;
		}
		if(null == i1){
			return i2;
		}
		if(null == i2){
			return i1;
		}
		return i1 + i2;
	}
	private CalculateUtils(){}
}
